package net.learn.piggy.aware;

import net.learn.piggy.event.MyApplicationEvent;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.*;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.metrics.ApplicationStartup;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;
import org.springframework.web.context.ServletContextAware;
import javax.servlet.ServletContext;
import java.io.IOException;

@Component
public class AwareConfig implements ApplicationContextAware, BeanFactoryAware, MessageSourceAware ,
        ResourceLoaderAware, ServletContextAware,
ApplicationEventPublisherAware,ApplicationStartupAware,EmbeddedValueResolverAware,
        EnvironmentAware, BeanClassLoaderAware, BeanNameAware {

    //3
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("AwareConfig -- BeanFactory");
    }

    // 10
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("AwareConfig -- ApplicationContext");
    }

    // 8
    @Override
    public void setMessageSource(MessageSource messageSource) {
        System.out.println("AwareConfig -- MessageSource");
    }

    // 6
    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        try {
            Resource resource = resourceLoader.getResource("classpath:META-INF");
            Resource relative = resource.createRelative("META-INF/spring.factories");
            System.out.println("AwareConfig -- ResourceLoader");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 11
    @Override
    public void setServletContext(ServletContext servletContext) {
        System.out.println("AwareConfig -- ServletContext");
    }

    // 7
    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        applicationEventPublisher.publishEvent(new MyApplicationEvent("My-Event"));
        System.out.println("AwareConfig -- ApplicationEventPublisher");
    }

    // 9
    @Override
    public void setApplicationStartup(ApplicationStartup applicationStartup) {
        System.out.println("AwareConfig -- ApplicationStartup");
    }

    // 5
    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        String name = resolver.resolveStringValue("${piggy.name}");
        System.out.println("AwareConfig -- StringValueResolver -- " + name);
    }

    // 4
    @Override
    public void setEnvironment(Environment environment) {
        System.out.println("AwareConfig -- Environment");
    }

    // 2
    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        System.out.println("AwareConfig -- ClassLoader");
    }

    // 1
    @Override
    public void setBeanName(String s) {
        System.out.println("AwareConfig -- setBeanName -- " + s);
    }


}
